<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>添加任务</title>
  
  <#import "/public/common.ftl" as common>
  <@common.plan_css datepicker=true page=true/>
  <@common.plan_js datepicker=true form=true/>

  <script type="text/javascript">
    $(function(){
      $('#endTime, #startTime').datepicker({format:'yyyy-mm-dd'});

      var bp = ${bp_json};
      var bid = 0;
      <#if (plan.bid)??>var bid = ${plan.bid};</#if>
      var sid = 0;
      <#if (plan.sid)??>var sid = ${plan.sid};</#if>
      var selectb = '';
      var cmap = {};
      $.each(bp,function(i,node) {
        var html = '<option value="'+node.id+'"';
        if(bid==node.id) {
          html += ' selected';
        }
        html += '>'+node.name+'</option>';
        selectb += html;
        cmap[node.id] = node.children;
      });
      $('#bid').append(selectb);

      var first = true;
      var changeSid = function() {
        var list = cmap[$('#bid').val()];
        var selects = '';
        $.each(list,function(i,node) {
          var html = '<option value="'+node.id+'"';
          if(first) {
            // 第一次处理原来选择的项目
            if(sid==node.id) {
              html += ' selected';  
            }
          }
          html += '>'+node.name+'</option>';
          selects += html;
        });
        $('#sid').empty();
        $('#sid').append(selects);
        first = false;
      }

      $('#bid').change(changeSid);
      changeSid();
      

      $('#myform').submit(function() {
        // 提交表单
        $(this).ajaxSubmit({  
           //target: '#output',          //把服务器返回的内容放入id为output的元素中      
           //beforeSubmit: showRequest,  //提交前的回调函数  
           type : 'post',
           dataType : 'json',
           success: function(data){         //提交后的回调函数  
            if(data.code==-1) {
              alert(data.message);
            } else if(data.code==-2) {
              alert('请登陆');
            } else if(data.code==0) {
              location.href = '${base}/work/plan/add';
            } else {
              alert('操作失败');
            }
           }
           //url: url,                 //默认是form的action， 如果申明，则会覆盖  
           //type: type,               //默认是form的method（get or post），如果申明，则会覆盖  
           //dataType: null,           //html(默认), xml, script, json...接受服务端返回的类型  
           //clearForm: true,          //成功提交后，清除所有表单元素的值  
           //resetForm: true,          //成功提交后，重置所有表单元素的值  
           // timeout: 3000               //限制请求的时间，当请求大于3秒后，跳出请求  
        }  );
        // 为了防止普通浏览器进行表单提交和产生页面导航（防止页面刷新？）返回false
        return false;
       });
    })
  </script>

  </head>

  <body>
      <div class="center w100pre bs-example" >
        <form method="post" id="myform" action="${base}/work/plan/save.json" class="form-horizontal w500 m-top30">
          <input type="hidden" name="id" value="${(plan.id)!}"/>
          <div class="form-group">
            <label class="col-sm-2 control-label">大项目</label>
            <div class="col-sm-10">
              <select class="form-control" name="bid" id="bid">
                </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">小项目</label>
            <div class="col-sm-10">
              <select class="form-control" name="sid" id="sid">
                </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">进度</label>
            <div class="col-sm-10">
              <@common.plan_select_progress progress="${(plan.progress)!}" name="progress" clazz="form-control"/>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">优先级</label>
            <div class="col-sm-10">
              <@common.plan_select_priority priority="${(plan.priority)!}" name="priority" clazz="form-control"/>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">开始时间</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" 
                  name="startTime" id="startTime" readonly="readonly" value="<#if (plan.startTime)??>${plan.startTime?string("yyyy-MM-dd")}</#if>">
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">结束时间</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"
                name="endTime" id="endTime" readonly="readonly" value="<#if (plan.endTime)??>${plan.endTime?string("yyyy-MM-dd")}</#if>">
            </div>
          </div>


		<div class="form-group">
            <label class="col-sm-2 control-label">标题</label>
            <div class="col-sm-10">
              <input type="text" class="form-control"
                name="title" value="${(plan.title)!}">
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">内容</label>
            <div class="col-sm-10">
               <textarea class="form-control" rows="10" name="content">${(plan.content)!}</textarea>
            </div>
          </div>

          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-default">提交</button>
            </div>
          </div>
        </form>
      </div>
  </body>
</html>